package learn_effective_java.item_50;

import java.util.Date;

// Broken "immutable" time period class
public final class Period {
	
	private final Date start;
	private final Date end;
	
	public Period(Date start, Date end) {
//		if(start.compareTo(end) > 0) {
//			throw new IllegalArgumentException(start + " after " + end);
//		}
//		this.start = start;
//		this.end = end;
		
		// right way
		this.start = new Date(start.getTime());
		this.end = new Date(end.getTime());
		if(this.start.compareTo(end) > 0) {
			throw new IllegalArgumentException(start + " after " + end);
		}
	}
	
	public Date start() {
		return new Date(start.getTime());
	}
	
	public Date end() {
		return new Date(end.getTime());
	}
	
	@Override
	public String toString() {
		return "Period [start=" + start + ", end=" + end + "]";
	}

}
